﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.ServiceModel.Web;
using System.Web;
namespace RestService
{
    public class RestServiceImpl : IRestServiceImpl
    {

        #region IRestServiceImpl Members
        String serverVersion = "04";

        public RobotResponse robotGeneralInfo(string type)
        {
            RobotFanuc fanuc = RobotFanuc.getRobot();            
            LogRecording.LogMessage("Info requested. robotGeneralInfo(+ " + type +")");
            return fanuc.robotGeneralInfo(type);
        }

        public RobotResponse ping()
        {
            RobotResponse rr = new RobotResponse();
            try
            {                
                LogRecording.LogMessage("Ping. ping()");
                rr.setMessage("Pong (server v." + serverVersion + ")"); 
                
            } catch (Exception e) {
                rr.setMessage(e.Message);
                
            }
            return rr;
           
        }
        public void robotConnect_post(Stream body)
        {
            robotConnect(body);
        }
        public void robotConnect(Stream body)
        {
            RobotFanuc fanuc = RobotFanuc.getRobot();            
            LogRecording.LogMessage("Connection requested. robotConnect()");
            string rawString = new StreamReader(body).ReadToEnd();
            NameValueCollection queryString = HttpUtility.ParseQueryString(rawString);
            string ip = queryString.Get("ip");
            if (string.IsNullOrEmpty(ip))
            {
                ip = fanuc.getDefaultRobotIP();
            }
            try {
               fanuc.connect(ip);
               WebOperationContext ctx = WebOperationContext.Current;
               ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created; // return a 201 status code "CREATED"
            } catch (Exception e) {
               WebOperationContext ctx = WebOperationContext.Current;
               ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; // return a 500 "Internal Server Error"
               ctx.OutgoingResponse.StatusDescription = e.Message;
            }            
        }

        public RobotResponse robotIsConnected()
        {
            RobotFanuc fanuc = RobotFanuc.getRobot();
            RobotResponse rr = new RobotResponse();
             try
             {
                 if (fanuc.isConnected())
                 {
                    rr.setMessage("1");
                 } else {
                    rr.setMessage("0");
                 }
             } catch (Exception e) {
                 WebOperationContext ctx = WebOperationContext.Current;
                 ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; 
                 ctx.OutgoingResponse.StatusDescription = e.Message;                 
             }
             return rr;
        }


        public RobotPosition robotGetPosition(string format)
        {
            RobotJointsValues joints = new RobotJointsValues();
            RobotXYZWPRValues xyz = new RobotXYZWPRValues();
            RobotPosition position = null;
            //string format = robotString.getString();
            try 
            {   RobotFanuc fanuc = RobotFanuc.getRobot();
                switch (format)
                {
                    case "joint":
                        joints = fanuc.robotCurrentJointsValues();
                        position = new RobotPosition(joints);
                        break;
                    case "XYZ":
                        xyz = fanuc.robotCurrentXYZWPRValues();
                        position = new RobotPosition(xyz);
                        break;
                    default:
                        setErrorStatus("Position format requested not recognised");                                                  
                        break;
                }            
             } catch (Exception e) {
                 setErrorStatus(e.Message);                 
             }
            return position;
        }


        public void robotMoveToPosition(Stream body, string format)
        {

            RobotFanuc fanuc = RobotFanuc.getRobot();
            string rawString = new StreamReader(body).ReadToEnd();
            NameValueCollection queryString = HttpUtility.ParseQueryString(rawString);

            switch (format)
            {
                case "joint":
                   RobotJointsValues joints = new RobotJointsValues(queryString.Get("joint1"), queryString.Get("joint2"), queryString.Get("joint3"), queryString.Get("joint4"), queryString.Get("joint5"), queryString.Get("joint6"));
                    fanuc.robotMoveToJoints(joints);
                    break;
                case "XYZ":
                    RobotXYZWPRValues xyzWpr = new RobotXYZWPRValues(queryString.Get("x"), queryString.Get("y"), queryString.Get("z"), queryString.Get("w"), queryString.Get("p"), queryString.Get("r"));
                    fanuc.robotMoveToXYZWPR(xyzWpr);                   
                    break;
                default:
                    setErrorStatus("New position format requested not recognised");
                    break;
            }                       
        }
        

        private void setErrorStatus(string message)
        {
            WebOperationContext ctx = WebOperationContext.Current;
            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; 
            ctx.OutgoingResponse.StatusDescription = message;   
        }

       
        public RobotJointsValues robotCurrentJoints()
        {
            RobotFanuc fanuc = RobotFanuc.getRobot();
            RobotJointsValues rjv = fanuc.robotCurrentJoints();
            List<RobotJointsValues> lrjv = new List<RobotJointsValues>();
            lrjv.Add(rjv);
            return rjv;
        }
        
        public string robotCurrentJointsString()
        {
            return "You requested joints " ;
        }
                              
        #endregion
    }
}